<?php
/**
 +------------------------------------------------------------------------------
 * 分页类
 +------------------------------------------------------------------------------
 * @name Page.class.php
 * @author 李枨煊[lcx165@gmail.com]
 * @copyright (C)2009 李枨煊
 * @version Wed Dec 10 14:52:28 CST 2008
 */

class Page {
	public $page;	//当前页
	public $limit;	//每页多少项
	public $total;	//一共多少条数据
	public $url;	//URL模式
	public $maxNum;	//排列显示多少页
	private $pageCount=0;	//一共多少页
	
	/**
	 * 构造函数，内部覆值
	 *
	 * @param 当前页数 $page
	 * @param 每页多少项 $limit
	 * @param 总共多少页 $max
	 * @param 排列显示多少页(不能小于4) $maxNum
	 * @param 地址 $url
	 */
	public function __construct($page, $limit, $total, $url, $maxNum=10) {
		$this->page = (int)$page;
		$this->limit = (int)$limit;
		$this->total = (int)$total;
		$this->maxNum = (int)$maxNum;
		$this->url = $url;
	}
	
	/**
	 * 返回分页信息
	 *
	 * @return 分页信息
	 */
	public function fetch() {
		$result = $this->getShowPageNum();	//取得要显示的页数		
		
		$display = "[总计:{$this->total}][{$this->page}/{$this->pageCount}]";
		
		//首页
		$url = str_replace('|page|', '1', $this->url);
		$display .= "<a href='$url'>[首页]</a>";
		
		if($this->page > 1) {	//上一页		
			$url = str_replace('|page|', $this->page-1, $this->url);
			$display .= "<a href='$url'>[上一页]</a>";
		}
		
		foreach ($result as $value) {	//显示分页列表		
			if($value == $this->page) {	//当前页			
				$display .= "[$value]";
			}
			else {	//非当前页			
				$url = str_replace('|page|', $value, $this->url);
				$display .= "[<a href='$url'>{$value}</a>]";
			}
		}
		
		if($this->page < $this->pageCount) {	//下一页		
			$url = str_replace('|page|', $this->page+1, $this->url);
			$display .= "[<a href='{$url}'>下一页</a>]";
		}
		
		//尾页
		$url = str_replace('|page|', $this->pageCount, $this->url);
		$display .= "<a href='$url'>[尾页]</a>";
		
		//跳到指定页函数
		$display .= "<script type='text/javascript'>
		function pageGoto(\$pg) {		
			var \$url = '{$this->url}';
			
			if(\$url.substr(0,11) == 'javascript:') {			
				eval(\$url.replace('|page|', \$pg));
			}
			else {			
				location.href= \$url.replace('|page|', \$pg);
			}
		}
		</script>
		";
		
		//输入框
		$display .= "<span><input type='text' style='width:30px' onkeydown='if(event.keyCode==13) pageGoto(this.value)' />";
		$display .= "<input type='button' value='GO' onclick='pageGoto(this.parentNode.getElementsByTagName(\"input\")[0].value)' /></span>";
		
		return $display;
	}
	
	//直接显示分页信息
	public function display() {
		echo $this->fetch();
	}
	
	/**
	 * 计算总数页
	 *
	 * @return 结果
	 */
	public function getPageCount() {
		//查询之前是否计算过，有取，没有算。
		if(!$this->pageCount) {
			$this->pageCount = ceil($this->total / $this->limit);
		}
		return $this->pageCount;
	}
	
	/**
	 * 取得要显示的页数
	 *
	 * @return array
	 */
	private function getShowPageNum() {
		$result = array();
		$this->getPageCount();	//取得要显示多少页
		
		if($this->pageCount <= $this->maxNum || $this->page <= 4) {	//总页数不足最多页或当前页没到第四页，显示最多页		
			$start = 1;
			$max = min($this->pageCount, $this->maxNum);	//总数与指定最多页数哪个小使用哪个
		}
		elseif($this->page > $this->pageCount - $this->maxNum + 4) {	//从后显示最后的		
			$start = $this->pageCount - $this->maxNum + 1;
			$max = $this->pageCount;
		}
		else {		
			$start = $this->page - 3;
			$max = $this->page - 3 + $this->maxNum - 1;
		}
		
		for($i=$start; $i<=$max; $i++) {	//开始生成		
			$result[] = $i;
		}
		
		return $result;
	}
	
	
}
/*
$pg = isset($_GET['pg']) ? (int)$_GET['pg'] : 1;
$page = new Page($pg, 10, 2000, '?pg=|page|');
$page->display();
*/


?>